<template>
  <div class="view_page userList">
    <el-card class="box-card">
      <!--卡片头部-->
      <div slot="header" class="clearfix">
        <!--左侧-->
        <div class="left">
          <div class="menu-title">
            气泡管理
          </div>
        </div>
        <!--右侧-->
        <div class="right"></div>
      </div>
      <!-- 卡片内容 -->
      <div>
        <!--筛选-->
        <el-table :data="datalist" style="width: 100%">
          <el-table-column prop="id" label="ID" align="center"></el-table-column>
          <el-table-column prop="name" label="名称" align="center"></el-table-column>
          <el-table-column prop="bubble_icon_url" label="气泡图标" align="center">
            <template slot-scope="scope">
              <img v-if="scope.row.bubble_icon_url != ''" :src="scope.row.bubble_icon_url" alt=""
                   class="list_img"/>
              <img v-else :src="defaultImg" alt="" class="list_img"/>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="200" align="center">
            <template slot-scope="scope">
              <div style="display: flex;align-items: center;flex-wrap: wrap;">
                <el-button type="primary" size="small" @click="navtoAdd(scope.row)">修改气泡</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <!--        <pagination
                  :total="total"
                  :currentPage="pageNum"
                  :pageSize="size"
                  position="right"
                  @search="searchPagination"
                >
                </pagination>-->
      </div>
    </el-card>
    <el-dialog title="修改气泡" :visible.sync="dialogVisible" width="50%">
      <el-form :model="liveData" label-width="100px">
        <el-form-item label="名称" :required="false">
          <el-input v-model="liveData.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="图标" label-width="100px" :required="true">
          <el-upload class="upload-demo" ref="upload0" :action="uploadUrl" :on-preview="handlePreview"
                     :on-remove="handleRemove" :file-list="fileList" :data="uploadData"
                     :before-upload="beforeAvatarUpload" :on-success="handleAvatarSuccess"
                     :on-error="handleAvatarError" :on-exceed="numberExceeded" :auto-upload="true"
                     accept=".png,.jpg,.jpeg" :limit="1">
            <el-button slot="trigger" size="small" type="primary">选取文件
            </el-button>
          </el-upload>
          <div>
            <img :src="file_image" alt="" v-if=" file_image != '' && file_image != null "
                 style="width: 100px; object-fit: cover;margin-top: 5px;"/>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs";
import {Message} from "element-ui";
import pagination from "@/components/common/pagination/pagination";
import devConfig from "*/dev.config";

export default {
  components: {
    pagination
  },
  watch: {},
  data() {
    return {
      defaultImg: 'https://dongping.jnaw.top/dongping_static/defaultImg.png',
      // 筛选内容
      TESTFROMDATA: {
        title: ""
      },
      dialogVisible: false,
      uploadUrl: this.$apis.getUploadUrl() + "/file/upload", //上传地址
      // 列表的数据
      datalist: [
        // {
        //   id: 1,
        //   name: "监控",
        //   bubble_icon: 5270,
        //   bubble_icon_url: "https://dongping.jnaw.top/smxc_photo/2023052413323322.png",
        //   bubble_icon_name: "放水洞.png",
        // },
        // {
        //   id: 2,
        //   name: "全景",
        //   bubble_icon: 5275,
        //   bubble_icon_url: "https://dongping.jnaw.top/smxc_photo/2023052413333010.png",
        //   bubble_icon_name: "全景.png",
        // },
      ],
      pageNum: 1,
      page: 1,
      size: 10,
      total: 0,

      liveData: {},//详情
      file_id: '',
      fileList: [],
      file_image: '',
      uploadData: {
        type: 1 //没用
      },
    };
  },
  created() {
    this.getListPage();
  },
  mounted() {
  },
  methods: {
    //查询列表
    getListPage() {
      var _this = this;
      var datas = {};
      _this.$apis.unity_bubble_list(datas).then(res => {
        if (res.result == 0) {
          _this.datalist = res.data;
        }
      });
    },
    //模糊查询
    searchList() {
      this.page = 1;
      this.datalist = [];
      this.getListPage();
    },
    //分页
    searchPagination(page) {
      this.page = page;
      localStorage.setItem("page", page);
      this.getListPage();
    },
    // 打开弹窗
    navtoAdd(item) {
      var _this = this;
      console.log(item)
      _this.liveData = JSON.parse(JSON.stringify(item))
      if (_this.liveData.bubble_icon_url != '' && _this.liveData.bubble_icon_url != null) {
        _this.fileList = [{
          id: _this.liveData.bubble_icon || '',
          name: _this.liveData.bubble_icon_name || '',
          url: _this.liveData.bubble_icon_url || ''
        }]
        _this.file_image = _this.liveData.bubble_icon_url || ''
        _this.file_id = _this.liveData.bubble_icon || ''
      } else {
        _this.fileList = []
        _this.file_image = ''
        _this.file_id = ''
      }
      _this.dialogVisible = true;
    },
    //取消
    cancel() {
      this.dialogVisible = false;
    },
    save() {
      let _this = this;
      if (_this.file_id == '') {
        Message.warning('请上传图片')
        return;
      }
      let datas = {
        id: _this.liveData.id,
        file_id: _this.file_id
      }
      _this.$apis.unity_bubble_update(datas).then(res => {
        if (res.result == 0) {
          Message.success("添加成功");
          _this.dialogVisible = false;
          _this.getListPage();
        } else {
          _this.dialogVisible = false;
          _this.getListPage();
        }
      });
    },
    //上传文件
    submitUpload() {
      this.$refs.upload.submit();
    },
    //文件列表移除文件时的钩子
    handleRemove(file, fileList) {
      this.fileList = fileList
      this.file_id = ''
      this.file_image = "";
    },
    //点击文件列表中已上传的文件时的钩子
    handlePreview(file) {
      console.log(file);
    },
    //文件上传成功的钩子
    handleAvatarSuccess(res, file, fileList) {
      console.log(res);
      this.fileList = fileList
      this.file_id = res.data.id;
      this.file_image = res.data.url
    },
    //文件上传失败时的钩子
    handleAvatarError(err, file) {
      Message.error("文件上传失败，请稍后再试");
    },
    //文件超出一个
    numberExceeded() {
      Message.warning("只能上传一张图片");
    },
    //上传文件之前的钩子
    beforeAvatarUpload(file) {
      var typeList = ["image/jpeg", "image/png", "image/jpg"];
      // const isJPG = file.type === 'image/jpeg'
      const isJPG = typeList.includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isJPG) {
        this.$message.warning("上传图片只能是 JPG或者PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.warning("上传文件大小不能超过 10MB!");
      }
      return isJPG && isLt2M;
    },
  }
};
</script>

<style>
.el-table .cell {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal !important;
}

.be_careful {
  color: red;
  margin-left: 22%;
}

.list_img {
  width: 80px;
  height: 80px;
  object-fit: cover;
}
</style>
